<?php

namespace app\index\controller;

use app\admin\model\City;
use app\admin\model\Meiti;
use app\index\model\Creditmoney;
use app\index\model\Creditmonth;
use app\index\model\Creditjob;
use app\index\model\Flink;
use app\index\model\Pic;
use app\index\model\Zizhi;
use app\index\model\Artcate;
use app\index\model\Article;
use app\index\model\Car;
use app\index\model\Creditcate;
use app\index\model\House;
use app\index\model\Webcity;
use app\index\model\Xinyong;
use app\index\model\Consultant;
use app\index\model\Bank;
use app\index\model\Question;
use app\index\controller\CommonController;
use think\Cache;

class IndexController extends CommonController
{
    public function index()
    {
        //轮播图
        $picList = Pic::where('type', '=', Pic::TYPE_1)->select();

        //信用贷类型
        $creditcateList = Creditcate::order('id asc')->select();

        //信用贷
        $xinyongList = [];
        if (count($creditcateList) > 0) {
            foreach ($creditcateList as $key => $value) {
                $oneList = Xinyong::where('creditcate_id', '=', $value['id'])
                    ->field('id, name, description, img')
                    ->order('add_time desc')
                    ->limit(0, 5)
                    ->select();

                $xinyongList[$key] = $oneList;
            }
        }

        //房抵贷
        $houseList = House::field('id, name, description, img')
            ->order('add_time desc')
            ->limit(0, 5)
            ->select();

        //车抵贷
        $carList = Car::field('id, name, description, img')
            ->order('add_time desc')
            ->limit(0, 5)
            ->select();

        //贷款攻略分类
        $artcateList = Artcate::field('id, name')
            ->order('id asc')
            ->select();

        //贷款攻略
        $articleList = [];
        if (count($artcateList) > 0) {
            foreach ($artcateList as $key => $value) {
                $oneList = Article::where('artcate_id', '=', $value['id'])
                    ->field('id, title, img')
                    ->order('add_time desc')
                    ->limit(0, 16)
                    ->select();

                $articleList[$key] = $oneList;
            }
        }

        //资质认证
        $zizhiList = Cache::get('zizhi', null);
        if (is_null($zizhiList)) {
            $zizhiList = Zizhi::order('add_time asc')->select();

            Cache::set('zizhi', $zizhiList);
        }

        //城市贷款
        $webcityList = Cache::get('webcity', null);
        if (is_null($webcityList)) {
            $webcityList = Webcity::where('is_open', '=', Webcity::IS_OPEN_1)
                ->field('id, city, pinyin')
                ->order('add_time asc')
                ->select();
            foreach ($webcityList as $key => $value) {
                $webcityList[$key]['pinyin'] = strtolower($value['pinyin']);
            }

            Cache::set('webcity', $webcityList);
        }

        //友情链接
        $flinkList = Cache::get('flink', null);
        if (is_null($flinkList)) {
            $flinkList = Flink::order('add_time asc')->select();

            Cache::set('flink', $flinkList);
        }

        //贷款金额
        $creditmoneyList = Creditmoney::order('id asc')
            ->select();

        //贷款期限
        $creditmonthList = Creditmonth::order('id asc')
            ->select();

        //职业身份
        $creditjobList = Creditjob::order('id asc')
            ->select();
        //媒体报道
        $meitiList=Meiti::order('id desc')
            ->select();
        //zcw 职业顾问
        $consultantList = Consultant::order('id asc')
            ->limit(0, 4)
            ->select();
        //银行
        $bankList = Bank::order('id asc')
            ->select();


        $questiontype0 = Question::field('id, question')
            ->where('questiontype','=',0)
            ->select();
        $questiontype1 = Question::field('id, question')
            ->where('questiontype','=',1)
            ->select();
        $questiontype2 = Question::field('id, question')
            ->where('questiontype','=',2)
            ->select();
        $questiontype3 = Question::field('id, question')
            ->where('questiontype','=',3)
            ->select();
        return view('index', [
            'menuIndex' => 1,
            'picList' => $picList,
            'creditcateList' => $creditcateList,
            'xinyongList' => $xinyongList,
            'houseList' => $houseList,
            'carList' => $carList,
            'artcateList' => $artcateList,
            'articleList' => $articleList,
            'zizhiList' => $zizhiList,
            'webcityList' => $webcityList,
            'flinkList' => $flinkList,
            'creditmoneyList' => $creditmoneyList,
            'creditmonthList' => $creditmonthList,
            'creditjobList' => $creditjobList,
            'meitiList'=>$meitiList,
            'consultantList' => $consultantList,
            'bankList' =>$bankList,


            'questiontype0' => $questiontype0,
            'questiontype1' => $questiontype1,
            'questiontype2' => $questiontype2,
            'questiontype3' => $questiontype3,
        ]);
    }
    public function guwen()
    {
        //zcw 职业顾问
        $id = input('param.id');
        $consultantList = Consultant::where('id', '=', $id)
            ->select();
        return view('guwen', [
            'menuIndex' =>1,
            'consultantList' => $consultantList,
        ]);
    }

    public function bank()
    {
        $id = input('param.id');
        $row = Article::with('artcate')->where('id', '=', $id)->find();

        //相关阅读
        $relateList = Article::where('artcate_id', '=', $row['artcate_id'])
            ->where('id', '<>', $row['id'])
            ->order('add_time desc')
            ->limit(0, 6)
            ->select();

        //上一篇
        $preRow = Article::where('artcate_id', '=', $row['artcate_id'])
            ->where('id', '<', $row['id'])
            ->find();

        //下一篇
        $nextRow = Article::where('artcate_id', '=', $row['artcate_id'])
            ->where('id', '>', $row['id'])
            ->find();
        //银行信息
        $bankList = Bank::where('id', '=', $id)
            ->select();
        //合作银行
        $bankLists = Bank::order('id asc')
            ->select();
        //银行产品
        $messages = House::order('id asc')
            ->where('typeid','=',$id)
            ->select();

        return view('content', [
            'menuIndex' => 1,
            'row' => $row,
            'relateList' => $relateList,
            'preRow' => $preRow,
            'nextRow' => $nextRow,
            'bankList' => $bankList,
            'bankLists' => $bankLists,
            'messages' =>$messages,
        ]);

    }
}
